package com.xuqi.Game;

import java.util.ArrayList;

import com.xuqi.menucontrol.MenuControlIF;

import android.R;
import android.R.integer;
import android.app.PendingIntent.CanceledException;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.location.Address;
import android.util.Log;
import android.graphics.PorterDuff;

public class GameElement implements GameDrawElemIF
{

	private final static String TAG = "GameElement";
	public GameElement()
	{
		super();
		Log.d(TAG, "GameElement()");
		// TODO Auto-generated constructor stub
	}
	public GameElement(Bitmap bmp,ELoaction _location)
	{
		super();
		Log.d(TAG, " GameElement(Bitmap bmp,ELoaction _location)");
		if (bmp != null)
		{
			this.SetImage(bmp);
		}
		if(_location != null)
		{
			this.SetElemLoc(_location);
		}
	}
	
	public GameElement(ESize _size, ELoaction _location)
	{
		super();
		Log.d(TAG, "GameElement(ESize _size, ELoaction _location)");
		if (_size != null)
		{
			this.SetElemSize(_size);
		}
		if (_location != null)
		{
			this.SetElemLoc(_location);
		}
		// TODO Auto-generated constructor stub
	}

	public GameElement(ESize _size, ELoaction _location, int _color)
	{
		this(_size, _location);
		Log.d(TAG, "GameElement(ESize _size, ELoaction _location, int _color)");
		if (_color != -1)
		{
			this.SetElemColor(_color);
		}
		// TODO Auto-generated constructor stub
	}

	@Override
	public void GDraw(Canvas _canvas)
	{
		Log.d(TAG, "GDraw");
		// TODO Auto-generated method stub
		this.DrawElem(_canvas);
		for(int i = 0; i < m_ElemFrameList.size(); i++)
		{
			m_ElemFrameList.get(i).GDraw(_canvas);
		}
	}
	@Override
	public void GClear(Canvas _canvas)
	{
		_canvas.drawColor(Color.TRANSPARENT,PorterDuff.Mode.CLEAR);
	}
	protected void DrawElem(Canvas _canvas)
	{
		Log.d(TAG, "X:"+m_cElemSize.width + "\tY:" + m_cElemSize.height);
		//this.DrawBG(_canvas);
		this.DrawFront(_canvas);
		this.DrawImage(_canvas);
	}
	protected void DrawFront(Canvas _cCanvas)
	{
		Bitmap BG = Bitmap.createBitmap(m_cElemSize.width-4, m_cElemSize.height-4, Config.ARGB_8888);
		Canvas canvas = new Canvas(BG);
		canvas.drawColor(m_cElemColor);
		canvas.save();
		_cCanvas.drawBitmap(BG, m_cElemLoaction.left+2, m_cElemLoaction.top+2, null);
	}

	public void DrawBG(Canvas _cCanvas)
	{
		Bitmap BG = Bitmap.createBitmap(m_cElemSize.width, m_cElemSize.height, Config.ARGB_8888);
		Canvas canvas = new Canvas(BG);
		canvas.drawColor(m_cElemColor);
		canvas.save();
		_cCanvas.drawBitmap(BG, m_cElemLoaction.left, m_cElemLoaction.top, null);
	}
	
	public void DrawImage(Canvas _cCanvas)
	{
		if(m_cBitmap != null)
		{
			Rect srcRect = new Rect();
			srcRect.top = 0;
			srcRect.left = 0;
			srcRect.right = m_cBitmap.getWidth();
			srcRect.bottom = m_cBitmap.getHeight();
			
			Rect destRect = new Rect();
			destRect.top =  m_cElemLoaction.top;
			destRect.left = m_cElemLoaction.left;
			destRect.right = destRect.left + m_cElemSize.width;
			destRect.bottom = destRect.top + m_cElemSize.height;
			
			_cCanvas.drawBitmap(this.m_cBitmap, srcRect, destRect, null);
			//_cCanvas.drawBitmap(this.m_cBitmap, m_cElemLoaction.left, m_cElemLoaction.top, null);
		}
	}
	
	public ESize GetElemSize()
	{
		return this.m_cElemSize;
	}
	public GameDrawElemIF SetElemSize(ESize eSize)
	{
		this.m_cElemSize = eSize; 
		return this;
	}
	
	public GameDrawElemIF SetElemColor(int nColor)
	{
		this.m_cElemColor = nColor;
		return this;
	}
	
	public GameDrawElemIF SetElemLoc(ELoaction eLoaction)
	{
		this.m_cElemLoaction = eLoaction;
		return this;
	}
	
	public GameDrawElemIF SetImage(Bitmap bmp)
	{
		this.m_cBitmap = bmp;
		return this;
	}
	
	public GameDrawElemIF SetImage(int id)
	{
		SetImage(BitmapFactory.decodeResource(MenuControlIF.GetCurActivity().getResources(),id));
		return this;
		
	}
	@Override
	public GameDrawElemIF MoveLeftRight(int newLeft)
	{
		m_cElemLoaction.left = newLeft;
		return this;
		
	}
	@Override
	public GameDrawElemIF MoveUpDown(int newTop)
	{
		m_cElemLoaction.top = newTop;
		return this;
		
	}
	@Override
	public GameDrawElemIF SetLocation(int left, int top)
	{
		this.m_cElemLoaction.left = left;
		this.m_cElemLoaction.top = top;
		return this;
	}

	@Override
	public GameDrawElemIF SetSize(int nWidth, int nHeight)
	{
		// TODO Auto-generated method stub
		this.m_cElemSize.width = nWidth;
		this.m_cElemSize.height = nHeight;
		return this;
	}
	@Override
	public void Add(GameDrawElemIF gameDrawElemIF)
	{
		m_ElemFrameList.add(gameDrawElemIF);
	}
	/************************************************/
	protected ELoaction m_cElemLoaction = new ELoaction(0, 0);
	protected ESize m_cElemSize = new ESize(20, 20);
	protected int m_cElemColor = Color.WHITE;
	protected Bitmap m_cBitmap = null;
	protected ArrayList<GameDrawElemIF> m_ElemFrameList = new ArrayList<GameDrawElemIF>();
	/************************************************/
	public static class ELoaction
	{
		public ELoaction(int _left, int _top)
		{
			super();
			this.left = _left;
			this.top = _top;
		}
		public int left = 0;
		public int top = 0;
		public ELoaction add(ELoaction loc)
		{
			this.left += loc.left;
			this.top += loc.top;
			return this;
		}
	}

	public static class ESize
	{

		public ESize(int _width, int _height)
		{
			super();
			this.width = _width;
			this.height = _height;
		}

		public int width = 0;
		public int height = 0;

	}





}
